package com.seal.commons.utils.id;

import com.seal.commons.utils.NetUtils;
import com.seal.commons.utils.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.UUID;

/**
 * 功能描述: ID生成类 <br/>
 *
 * @author huangyong
 * @date: 2020-06-15 21:15<br/>
 * @since JDK 1.8
 */
public class UniqueIdUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(UniqueIdUtils.class);

    private static final SnowflakeIdUtils snowflakeIdUtils = new SnowflakeIdUtils();

    private static final String ip = ip();

    public static String uuid(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }


    /**
     * id生成格式： yyMMddHHmmssSSS+15位随机数
     * @return
     */
    public static String randomSequenceId(){
        String date = String.format("%1$tY%1$tm%1$te%1$tH%1$tM%1$tS%1$tL", Calendar.getInstance());
        return date + RandomUtils.generateAlphaAndDigit(15);
    }

    /**
     * id 生成格式： yyyyMMddHHmmss+ 2位ip + 18位雪花算法id
     * @return
     */
    public static String snowflakeId(){
        String date = String.format("%1$ty%1$tm%1$te%1$tH%1$tM%1$tS", Calendar.getInstance());;
        return date + ip + snowflakeIdUtils.nextId();
    }


    private static String ip(){
        String address = NetUtils.getLocalAddress().getHostAddress();
        int idx = address.lastIndexOf(".");
        String ip = address.substring(idx+1);
        ip = ip.length() >= 2 ? address.substring(address.length()-2) : "0"+ip;
        return ip;
    }




}